/**
 * 代号:隐无为 2017：厚溥
 * 文件名：UserController.java
 * 创建人：柯栋
 * 日期：2017年12月5日
 * 修改人：
 * 描述：
 */
package org.kd.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.kd.common.BaseController;
import org.kd.model.User;
import org.kd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;

/**
 * 用途：业务模块名称
 * 
 * 作业
 * 
 * 1. 美化登录样式
 * 2. 账号密码不能空友好提示
 * 
 */
@Controller
@RequestMapping("/userController")
public class UserController extends BaseController {
	@Autowired
	UserService userService;
	
	
	@RequestMapping("/ajax_delete")
	@ResponseBody
	public String ajax_delete(Integer id) {
		userService.delete(id);
		return "{\"msg\":\"delete_sucess\"}";//  web-inf/helloWorld.jsp 
	}
	
	@RequestMapping("/ajax_queryById")
	@ResponseBody
	public User ajax_queryById(Integer id) {
		User user = userService.queryById(id);
		return user;//   
	}
	
	/**
	 *  @功能:更新or保存
	 *  @作者:jh @代号:甲骨兽进化
	 *  @时间:2017年12月23日
	 *  @param user
	 *  @return  
	 */
	@RequestMapping("/ajax_saveOrUpdate")
	@ResponseBody
	public String ajax_saveOrUpdate(User user) {
		if(user.getId()!=null){
			userService.update(user);
		}else {
			userService.save(user);
		}
		return "{\"msg\":\"sucess\"}"; 
	}
	
	
	@RequestMapping("/helloWorld")
	public String helloWorld(Model model,HttpServletRequest request) {
		model.addAttribute("message", "Hello World! 6666"); 
		request.setAttribute("kd", "你6的飞起");
		return "helloWorld";//  web-inf/helloWorld.jsp 
	}
	
	@RequestMapping("/ajax")
	@ResponseBody
	public String ajax(Integer id,User user) {
		System.out.println(id);
		System.out.println(user.getId());
		return "helloWorld";//  web-inf/helloWorld.jsp 
	}
	
	
	@RequestMapping("/login")
	@ResponseBody
	public String login(User user,HttpSession httpSession) {
		
		User u=userService.login(user);
		if(u!=null){
			httpSession.setAttribute("userinfo", u);
			return "{\"msg\":\"登录成功\"}";
		}else{
			return "{\"msg\":\"登录失败\"}";
		}
	}
	
	
	
	@RequestMapping("/list")
	public String list(HttpServletRequest request,Model model) {
		model.addAttribute("message", "Hello World! 6666"); 
		return "page/user/list";
	}
	
	@RequestMapping("/ajaxList")
	@ResponseBody
	public Object ajaxList() {
		List<User>list=new ArrayList<User>();
		PageHelper.startPage(1, 5);
		list = userService.queryAll();
		Map<String, Object> jsonMap=new HashMap<String, Object>();
		jsonMap.put("list", list);
		return jsonMap;
	}
	
	
	
}
